//
//  FortunePaperViewController.m
//  SmileWeather
//
//  Created by lanou3g on 15/9/22.
//  Copyright (c) 2015年 陶天海. All rights reserved.
//

#import "FortunePaperViewController.h"
#import "FortunePaperCollectionViewCell.h"
#import "FortunePaperModel.h"
#import "PaperDetailViewController.h"
@interface FortunePaperViewController ()
@property (nonatomic, strong)NSMutableArray *paperArr;
@property (nonatomic, strong)UICollectionView *collectionView;

@property (nonatomic, strong)UIAlertView *alert;
@property (nonatomic, strong)UIActivityIndicatorView *act;

@property (nonatomic, assign)NSInteger LimNum;
@property (nonatomic, assign)NSInteger startNum;
@end

@implementation FortunePaperViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationItem.title = @"可爱壁纸";
    //创建布局对象
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置布局属性
    layout.minimumLineSpacing = 20;
    //提取高度
    CGFloat width = self.view.frame.size.width;
    
    layout.itemSize = CGSizeMake((width - 20) / 3, 200);
    
    //创建集合视图
   self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    //注册
    [self.collectionView registerClass:[FortunePaperCollectionViewCell class] forCellWithReuseIdentifier:@"myCell"];
    [self.view addSubview:_collectionView];
    self.LimNum = 9;
    self.startNum = 0;
    [self reloadPaper];
    
    self.alert = [[UIAlertView alloc] initWithTitle:@"正在加载..." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
    self.act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
    [_act setFrame:CGRectMake(120, 48, 37, 37)];
    [_alert addSubview:_act];
    [_act startAnimating];
    [_alert show];
    
    [self reloadPages];
}
//解析数据
- (void)reloadPaper{
    NSString *url = [NSString stringWithFormat:@"%@%ld%@%ld",kFortunePaper,self.LimNum,kFortunePaperrr,self.startNum];
  [LORequestManger GET:url success:^(id response) {
      NSDictionary *dic = (NSDictionary *)response;
      NSArray *object_list = dic[@"data"][@"object_list"];
      //FortunePaperModel *paperModel = self.paperArr.lastObject;
//
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"亲,已经到底了" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
//        [alert show];
   
      for (NSDictionary *photoDic in object_list) {
          NSDictionary *path = photoDic[@"photo"];
      FortunePaperModel *model = [[FortunePaperModel alloc] init];
          [model setValuesForKeysWithDictionary:path];
          [self.paperArr addObject:model];
              }
      [self.collectionView reloadData];
      [_act stopAnimating];
      [_alert dismissWithClickedButtonIndex:0 animated:YES];
  } failure:nil];
}
//懒加载
- (NSMutableArray *)paperArr{
    if (!_paperArr) {
        _paperArr = [[NSMutableArray alloc] init];
    }
    return _paperArr;
}
//下拉刷新
- (void)reloadPages{
    __block FortunePaperViewController *start = self;
    [self.collectionView addFooterWithCallback:^{
        start.LimNum += 9;
        start.startNum += 9;
        start.paperArr = nil;
        //重新解析数据
        [start reloadPaper];
        //刷新(解析数据有刷新)
       //[start.collectionView reloadData];
        //停止刷新
        [start.collectionView footerEndRefreshing];

    }];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.paperArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    FortunePaperCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];
    //获取模型
    if (self.paperArr.count > 0) {
        FortunePaperModel *paperModel = self.paperArr[indexPath.row];
        [cell.headerView sd_setImageWithURL:[NSURL URLWithString:paperModel.path]placeholderImage:[UIImage imageNamed:@"ipad_bg"]];
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    PaperDetailViewController *paperVC = [[PaperDetailViewController alloc] init];
    FortunePaperModel *paperModel = self.paperArr[indexPath.row];
    paperVC.model = paperModel;
    [self.navigationController pushViewController:paperVC animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
